package gbackup.main;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.google.gdata.client.docs.DocsService;
import com.google.gdata.util.AuthenticationException;

import core.GBackupService;
import core.Shell;

public class ConsoleBasedGBackup implements GBackupService {
	private static final String VERSION = "OpenPen-ConsoleBasedGBackup-1.0";
	private Shell _shell = null;
	private String _username = null;
	private String _password = null;
	
	public ConsoleBasedGBackup() {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		System.out.print("Username: ");
		System.out.flush();
		try {
			this._username = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		
		System.out.print("Password: ");
		System.out.flush();
		try {
			this._password = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		
		this.startService();
	}
	
	public static void main(String[] argv) {
		new ConsoleBasedGBackup();
	}

	@Override
	public void startService() {
		DocsService client = new DocsService(VERSION);

		try {
			client.setUserCredentials(this._username + "@gmail.com", this._password);
		} catch (AuthenticationException e) {
			e.printStackTrace();
		}
		
		this._shell = new PShell(this, client, System.in, System.out);
		this._shell.start();
	}

	@Override
	public void stopService() {
		this._shell.stop();
		this._shell = null;
	}

	@Override
	public void restartService() {
		this.stopService();
		this.startService();
	}

}
